'use strict';
var _ = require('lodash');
var async = require('async');
var roleService = require('./roleService.js');
var mongoConnector = require('../libs/mongodb/mongoConnector');
var soldierService = require('../services/soldierService.js');
var roleModel = require('../models/roleModel.js');
var skillModel = require('../models/skillModel.js');
var buildModel = require('../models/buildModel.js');
var soldierModel = require('../models/soldierModel.js');
var arenaModel = require('../models/arenaModel.js');
var techModel = require('../models/techModel.js');

var Role = mongoConnector().model('Role');
var Skill = mongoConnector().model('Skill');
var Build = mongoConnector().model('Build');
var Soldier = mongoConnector().model('Soldier');
var Tech = mongoConnector().model('Tech');

var schema_map = {
    main_power: Role,
    hero_power: Role,
    skill_power: Skill,
    build_power: Build,
    soldier_power: Soldier,
    tech_power: Tech
};

var model_map = {
    main_power: roleModel,
    hero_power: roleModel,
    skill_power: skillModel,
    build_power: buildModel,
    soldier_power: soldierModel,
    tech_power: techModel
};

/**
 * 战力排行榜
 * @param  {[type]} limit [description]
 * @param  {[type]} cb [description]
 */
exports.getFightRank = function (field, limit, callback) {
    var role_id_list = [];
    var soldier_map = {};
    var arena_map = {};
    var role_map = {};
    var rank_list = [];
    var where;

    var top_rank_list;
    async.waterfall(
        [
            function (cb) {
                // 取出排行信息
                _getFightPowerRank(field, limit, cb);
            },
            function (list, cb) {
                top_rank_list = list;
                _.forEach(top_rank_list, function (item) {
                    role_id_list.push(item.role_id);
                });

                where = {
                    role_id: {
                        $in: role_id_list
                    }
                };

                // 取出士兵信息
                soldierModel.findWhere(where, cb);
            },
            function (list, cb) {
                _.forEach(list, function (soldier) {
                    soldier_map[soldier.role_id] = soldier;
                });

                // 取出竞技场信息
                arenaModel.findWhere(where, cb);
            },
            function (list, cb) {
                _.forEach(list, function (arena) {
                    arena_map[arena.role_id] = arena;
                });

                // 取出角色信息
                roleService.getRoleList(role_id_list, cb);
            },
            function (list, cb) {
                _.forEach(list, function (role) {
                    role_map[role.role_id] = role;
                });

                // 组合结果信息
                var rank_num = 0;
                _.forEach(top_rank_list, function (rank_info) {
                    rank_num++;
                    var role_id = rank_info.role_id;
                    var role_info = role_map[role_id];
                    var soldier_model = soldier_map[role_id];
                    var arena_model = arena_map[role_id];

                    // role_info[field] = rank_info[field];
                    role_info.fight_power = rank_info[field];
                    role_info.rank = rank_num;
                    if (!!arena_model) {
                        role_info.arena_rank = arena_model.rank;
                    } else {
                        role_info.arena_rank = 0;
                    }
                    role_info.soldier_list = soldierService.getSoldierInfoList(soldier_model.select_soldier_list, soldier_model);
                    rank_list.push(role_info);
                });

                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return callback(null, []);
            }

            callback(null, rank_list);
        }
    );
};

/**
 * 获取玩家战力排名
 * @param {[type]} current_role_rank [description]
 */
exports.getCurrentRoleRank = function (role_id, field, callback) {
    var current_role_info = {};
    var current_rank = 0;
    var soldier_model;
    async.waterfall(
        [
            function (cb) {
                soldierModel.getByRoleId(role_id, cb);
            },
            function (model, cb) {
                soldier_model = model;
                _getRoleFightPowerRank(role_id, field, cb);
            },
            function (rank, cb) {
                current_rank = rank;
                roleService.getRoleInfo(role_id, cb);
            },
            function (role_info, cb) {
                current_role_info = role_info;

                current_role_info.rank = current_rank;
                model_map[field].getByRoleId(role_id, cb);
            },
            function (model, cb) {
                // current_role_info[field] = model[field];
                current_role_info.fight_power = model[field];
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return callback(err);
            }
            return callback(null, current_role_info);
        }
    );
};

/**
 * 战力排行榜
 * @param  {[type]} limit [description]
 * @param  {[type]} cb [description]
 */
var _getFightPowerRank = function (field, limit, cb) {
    var field_list = {
        _id: 0,
        role_id: 1
    };
    field_list[field] = 1;

    var sort_list = {};
    sort_list[field] = -1;
    sort_list.role_id = -1;

    schema_map[field].find({}, field_list)
        .sort(sort_list)
        .limit(limit)
        .exec(cb);
};

/**
 * 查询玩家总战力排行
 * @param  {[type]} limit [description]
 * @param  {[type]} cb [description]
 */
var _getRoleFightPowerRank = function (role_id, field, callback) {
    async.waterfall(
        [
            function (cb) {
                schema_map[field].findOne({
                    role_id: role_id
                }, cb);
            },
            function (model, cb) {
                var power_val = model[field];
                var cnd_gt = {};
                var cnd_eq = {};
                cnd_gt[field] = {
                    $gt: power_val
                };
                cnd_eq[field] = power_val;
                cnd_eq.role_id = {
                    $gte: role_id
                };

                var cnd_or = [];
                cnd_or.push(cnd_gt);
                cnd_or.push(cnd_eq);

                var condition = {
                    $or: cnd_or
                };

                schema_map[field].find(condition)
                    .count()
                    .exec(cb);
            }
        ],
        function (err, rank_num) {
            if (!!err) {
                return callback(err, 0);
            }
            return callback(null, rank_num);
        }
    );
};
